package gui;

import javax.swing.JPanel;

import Entities.ExecutionEntity;
import Entities.InspectionEntity;
import Entities.RequestEntity;
import common.ChatIF;
import common.CommonMethods;

import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JComboBox;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

import javax.swing.JTextField;
import javax.swing.JCheckBox;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
/**
 * 
 * @author Idan Mann
 * This class is the GUI for the Inspection stage
 * It presents the Inspector with the execution details
 * and further more options
 *
 */
public class InspectPanel extends JPanel implements ChatIF
{
	private JComboBox comboBox;
	private JTextArea inspectiontextArea;
	private JButton btnSave;
	private JButton btnClosel;
	private JButton btnDeny;
	private JButton btnApprove;
	private JButton btnExtenddeadline;
	private JButton btnViewRequest;
	private JButton btnInspect;
	private CommitteeMemberPanel committeePanel;
	private AcademicFrame myframe;
	private InspectPanel myself;
	private JTextArea executiontextArea;
	private JScrollPane scrollPane_1;
	private JLabel lblInspectionDetails;
	private JLabel lblExecutionDetails;
	private ViewRequestPanel viewRequest;
	private String rid;
	private String exeID; 
	private JTextField deadlineTextField;

	
	public InspectPanel(AcademicFrame frame, CommitteeMemberPanel committeePanel)
	{
		
		this.myframe = frame;
		this.committeePanel = committeePanel;
		myframe.setSize(500, 400);
		this.myself = this;
	
		initialize();
	}
	
		
		public void initialize()
		{
		setLayout(null);
		
		JLabel lblInspection = new JLabel("Inspection");
		lblInspection.setFont(new Font("Tahoma", Font.PLAIN, 16));
		lblInspection.setBounds(265, 32, 89, 28);
		add(lblInspection);
		
		JLabel lblRequestNumber = new JLabel("Choose request");
		lblRequestNumber.setBounds(124, 87, 94, 14);
		add(lblRequestNumber);
		
		comboBox = new JComboBox();
		comboBox.setBounds(228, 83, 168, 22);
		add(comboBox);
		
		btnInspect = new JButton("Inspect");
		btnInspect.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e)
			{
				if ( comboBox.getSelectedItem().toString().equalsIgnoreCase("--Choose a request--"))
				{
					(new Dialog("Please select and item from the list first.")).setVisible(true);
				}
				else
				{
				rid = comboBox.getSelectedItem().toString();
				
				// Get execution report
				ArrayList<Object> getExecutionReport = new ArrayList<Object>();
				getExecutionReport.add("getExecutionReport");
				getExecutionReport.add(rid);
				AcademicFrame.myclient.sendMessageToServer(getExecutionReport);
				}
			}
		});
		btnInspect.setBounds(417, 83, 130, 23);
		add(btnInspect);
		
		btnApprove = new JButton("Approve");
		btnApprove.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e) 
			{
				ArrayList<Object> setInspectionData = new ArrayList<Object>();
				InspectionEntity inspectionToSet = new InspectionEntity();
				
				inspectionToSet.setFaults(inspectiontextArea.getText());
				inspectionToSet.setRequestID(rid);
				
				setInspectionData.add("setInspectionData");
				setInspectionData.add(inspectionToSet);
				setInspectionData.add("approve");
				AcademicFrame.myclient.sendMessageToServer(setInspectionData);
				
			}
		});
		btnApprove.setEnabled(false);
		btnApprove.setBounds(462, 436, 130, 23);
		add(btnApprove);
		
		btnClosel = new JButton("Close");
		btnClosel.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e) 
			{
				myframe.setSize(500, 500);
				myframe.setContentPane(committeePanel);		
			}
		});
		btnClosel.setBounds(71, 436, 130, 23);
		add(btnClosel);
		
		btnViewRequest = new JButton("View Request");
		
		btnViewRequest.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e)
			{
				if (viewRequest==null)
				{
					viewRequest = new ViewRequestPanel(rid,myframe,myself,false,"");
					myframe.setContentPane(viewRequest);
				}
				else
					if (!viewRequest.getrequestID().equalsIgnoreCase(rid))
					{
						viewRequest = new ViewRequestPanel(rid,myframe,myself,false,"");
						myframe.setContentPane(viewRequest);
					}
				myframe.setContentPane(viewRequest);
				myframe.setSize(550, 730);					
			}
		});
		
		btnViewRequest.setEnabled(false);
		btnViewRequest.setBounds(462, 335, 130, 23);
		add(btnViewRequest);
		
		deadlineTextField = new JTextField();
		deadlineTextField.setEditable(false);
		deadlineTextField.setBounds(355, 372, 91, 20);
		add(deadlineTextField);
		deadlineTextField.setColumns(10);
		
		JLabel lblDeadline = new JLabel("Deadline");
		lblDeadline.setBounds(299, 375, 55, 14);
		add(lblDeadline);
		
		btnExtenddeadline = new JButton("Extend deadline");
		btnExtenddeadline.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e) 
			{
				if (CommonMethods.exceededDeadline(deadlineTextField.getText()))
				{	
					(new Dialog ("Deadline has passed, cannot extend.")).setVisible(true);
					btnExtenddeadline.setEnabled(false);
				}
				else
					if (CommonMethods.moreThan3Days(deadlineTextField.getText()) )
					{
						(new Dialog ("More than 3 days to the deadline, cannot extend.")).setVisible(true);
						
						btnExtenddeadline.setEnabled(false);
					}
					else
					{
						btnInspect.setEnabled(false);
						comboBox.setEnabled(false);
						btnSave.setEnabled(false);
						ExtendDeadline extendFrame = new ExtendDeadline(deadlineTextField.getText(), btnExtenddeadline, rid, "12", "exectuioninspection", "wasExtended", "requestID2" );
						extendFrame.setVisible(true);
						extendFrame.setSize(760,400);
					}
				
			}
		});
		btnExtenddeadline.setEnabled(false);
		btnExtenddeadline.setBounds(462, 371, 130, 23);
		add(btnExtenddeadline);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(163, 137, 429, 90);
		add(scrollPane);
		
		inspectiontextArea = new JTextArea();
		scrollPane.setViewportView(inspectiontextArea);
		
		btnSave = new JButton("Save");
		btnSave.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e)
			{
				
				ArrayList<Object> setInspectionData = new ArrayList<Object>();
				InspectionEntity inspectionToSet = new InspectionEntity();
				
				inspectionToSet.setFaults(inspectiontextArea.getText());
				inspectionToSet.setRequestID(rid);
				setInspectionData.add("setInspectionData");
				setInspectionData.add(inspectionToSet);
				setInspectionData.add("save");
				
				AcademicFrame.myclient.sendMessageToServer(setInspectionData);
				
			}
		});
		btnSave.setEnabled(false);
		btnSave.setBounds(71, 372, 130, 23);
		add(btnSave);
		
		btnDeny = new JButton("Deny");
		btnDeny.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent arg0)
			{
				ArrayList<Object> denyRequest = new ArrayList<Object>();
				InspectionEntity inspectionToDeny = new InspectionEntity();
				
				inspectionToDeny.setFaults(inspectiontextArea.getText());
				inspectionToDeny.setRequestID(rid);
				denyRequest.add("setInspectionData");
				denyRequest.add(inspectionToDeny);
				denyRequest.add("deny");		
				
				AcademicFrame.myclient.sendMessageToServer(denyRequest);
				
				
			}
		});
		btnDeny.setEnabled(false);
		btnDeny.setBounds(316, 436, 130, 23);
		add(btnDeny);
		
		scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(163, 240, 429, 90);
		add(scrollPane_1);
		
		executiontextArea = new JTextArea();
		executiontextArea.setBackground(UIManager.getColor("TextArea.disabledBackground"));
		executiontextArea.setEditable(false);
		scrollPane_1.setViewportView(executiontextArea);
		
		lblInspectionDetails = new JLabel("Inspection details");
		lblInspectionDetails.setBounds(43, 143, 110, 14);
		add(lblInspectionDetails);
		
		lblExecutionDetails = new JLabel("Execution details");
		lblExecutionDetails.setBounds(43, 250, 110, 14);
		add(lblExecutionDetails);
	}

	@Override
	public void display(Object message)
	{
		ArrayList<Object> answer = new ArrayList<Object>();
		answer = (ArrayList<Object>)message;
		
		
		if (	((String)(answer.get(0))).equalsIgnoreCase("listOfInspectionableRequests"))
		{
			int numberOfRequests = Integer.parseInt((String)answer.get(1)); // get the amount of items to appear in the combobox
			this.comboBox.addItem("--Choose a request--");
			for (int i=0; i < numberOfRequests ; i++ )
			{
				this.comboBox.addItem((String)answer.get(i+2));  // add requests to the list
			}
		}
		else
			if (	((String)(answer.get(0))).equalsIgnoreCase("executionEntity"))
			{
				if (	((String)(answer.get(1))).equalsIgnoreCase("Valid"))
				{
					ExecutionEntity execution = ( (ExecutionEntity)(answer.get(2))	);
					executiontextArea.setText(execution.getExecutionDetails());
					
					
					ArrayList<Object> getInspectionData = new ArrayList<Object>();
					getInspectionData.add("getInspectionData");
					getInspectionData.add(rid);
					AcademicFrame.myclient.sendMessageToServer(getInspectionData);
	
				}
				else
				{
					(new Dialog ("Could not find execution for the request in the database.")).setVisible(true);
					myframe.setContentPane(committeePanel);
					myframe.setSize(500, 400);
				}

				
			}
			else 
				if (	((String)(answer.get(0))).equalsIgnoreCase("returnedInspection"))
				{
					
					if (	((String)(answer.get(1))).equalsIgnoreCase("Valid"))
					{
					
						InspectionEntity inspection = ( (InspectionEntity)(answer.get(2))	);
						if (inspection.getFaults().equalsIgnoreCase("none"))
							inspectiontextArea.setText("");
						else
							inspectiontextArea.setText(inspection.getFaults());
						
						deadlineTextField.setText(inspection.getDeadline());
						
						if (inspection.getWasExtended().equalsIgnoreCase("no") )
							btnExtenddeadline.setEnabled(true);
						else
							btnExtenddeadline.setEnabled(false);
						btnApprove.setEnabled(true);
						btnDeny.setEnabled(true);
						btnViewRequest.setEnabled(true);
						btnSave.setEnabled(true);
					}
					else
					{
						(new Dialog("Error retriving inspection data")).setVisible(true);
					}
					
				}
				else
					if (	((String)(answer.get(0))).equalsIgnoreCase("extentionRequestWasSent")	)
					{
						btnInspect.setEnabled(true);
						comboBox.setEnabled(true);
						btnSave.setEnabled(true);
					}
					else 
					if (	((String)(answer.get(0))).equalsIgnoreCase("inspectionSet")	)
					{
						if (	((String)(answer.get(1))).equalsIgnoreCase("Valid")	)
						{
							comboBox.removeItemAt(comboBox.getSelectedIndex());
							resetFields("Inspection was approved.");
						}
					}
					else 
						if (	((String)(answer.get(0))).equalsIgnoreCase("inspectionSaved")	)
						{
							if (	((String)(answer.get(1))).equalsIgnoreCase("Valid")	)
							{
								comboBox.setSelectedIndex(-1);
								resetFields("Inspection was saved.");
							}
						}
						else
							if (	((String)(answer.get(0))).equalsIgnoreCase("inspectionDenied")	)
							{
								if (	((String)(answer.get(1))).equalsIgnoreCase("Valid")	)
								{
									comboBox.removeItemAt(comboBox.getSelectedIndex());
									resetFields("Inspection was denied.");
								}
							}
						

	

	}
	
	private void resetFields (String dialogMessage)
	{
		inspectiontextArea.setText("");
		executiontextArea.setText("");
		deadlineTextField.setText("");
		btnApprove.setEnabled(false);
		btnDeny.setEnabled(false);
		btnExtenddeadline.setEnabled(false);
		btnSave.setEnabled(false);
		btnViewRequest.setEnabled(false);
		(new Dialog (dialogMessage)).setVisible(true);
	}
}
